package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import com.sinbader.xiaole.common.entity.BuyOrderInfo;
import com.sinbader.xiaole.common.mapper.BuyOrderInfoMapper;
import com.sinbader.xiaole.common.model.params.BuyOrderInfoParam;
import com.sinbader.xiaole.common.model.result.BuyOrderInfoResult;
import  com.sinbader.xiaole.common.service.BuyOrderInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 采购订单信息表 服务实现类
 */
@Service
public class BuyOrderInfoServiceImpl extends ServiceImpl<BuyOrderInfoMapper, BuyOrderInfo> implements BuyOrderInfoService {

    @Override
    public void add(BuyOrderInfoParam param){
        BuyOrderInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BuyOrderInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BuyOrderInfoParam param){
        BuyOrderInfo oldEntity = getOldEntity(param);
        BuyOrderInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BuyOrderInfoResult findBySpec(BuyOrderInfoParam param){
        return null;
    }

    @Override
    public List<BuyOrderInfoResult> findListBySpec(BuyOrderInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BuyOrderInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BuyOrderInfoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BuyOrderInfo getOldEntity(BuyOrderInfoParam param) {
        return this.getById(getKey(param));
    }

    private BuyOrderInfo getEntity(BuyOrderInfoParam param) {
        BuyOrderInfo entity = new BuyOrderInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
